define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'business/privatesea/index' + location.search,
                    add_url: 'business/privatesea/add',
                    edit_url: 'business/privatesea/edit',
                    del_url: 'business/privatesea/del',
                    info_url: 'business/info/index', //添加info菜单
                    table: 'business',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                fixedColumns: true,
                fixedRightNumber: 1,
                columns: [
                    [
                        { checkbox: true },
                        { field: 'mobile', title: __('Mobile'), operate: 'LIKE' },
                        { field: 'nickname', title: __('Nickname'), operate: 'LIKE' },
                        { field: 'avatar', title: __('Avatar'), operate: 'LIKE', events: Table.api.events.image, formatter: Table.api.formatter.image },
                        { field: 'gender', title: __('Gender'), searchList: { "0": __('Gender 0'), "1": __('Gender 1'), "2": __('Gender 2') }, formatter: Table.api.formatter.normal },
                        { field: 'source.name', title: __('BusinessSource') },
                        { field: 'deal', title: __('BusinessDeal'), searchList: { "0": __('BusinessDeal 0'), "1": __('BusinessDeal 1') }, formatter: Table.api.formatter.normal },
                        { field: 'provinces.name', title: __('Province'), operate: 'LIKE' },
                        { field: 'citys.name', title: __('City'), operate: 'LIKE' },
                        { field: 'districts.name', title: __('District'), operate: 'LIKE' },
                        { field: 'admin.nickname', title: __('BusinessAdminid') },
                        { field: 'money', title: __('Money'), operate: 'BETWEEN' },
                        { field: 'email', title: __('Email'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content },
                        { field: 'poster', title: __('BusinessPoster'), operate: 'LIKE', events: Table.api.events.image, formatter: Table.api.formatter.image },
                        { field: 'createtime', title: __('Createtime'), operate: 'RANGE', addclass: 'datetimerange', autocomplete: false, formatter: Table.api.formatter.datetime },
                        { field: 'auth', title: __('BusinessAuth'), searchList: { "0": __('BusinessAuth 0'), "1": __('BusinessAuth 1') }, formatter: Table.api.formatter.normal },
                        { field: 'parent.nickname', title: __('BusinessParent') },
                        {
                            field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate,
                            buttons: [
                                {
                                    name: 'info',
                                    title: '客户详情',
                                    classname: "btn btn-xs btn-success btn-dialog",
                                    icon: 'fa fa-eye',
                                    extend: 'data-toggle=\'tooltip\' data-area= \'["80%", "100%"]\'', //data-toggle是鼠标放到上面悬停，会弹出响应的信息。  data-area是设置弹出框的大小
                                    url: $.fn.bootstrapTable.defaults.extend.info_url, //info_url默认有ids参数传过去
                                }
                            ]
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        recyclebin: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    'dragsort_url': ''
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: 'business/privatesea/recyclebin' + location.search,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        { checkbox: true },
                        { field: 'id', title: __('Id') },
                        {
                            field: 'deletetime',
                            title: __('Deletetime'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            formatter: Table.api.formatter.datetime
                        },
                        {
                            field: 'operate',
                            width: '140px',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            buttons: [
                                {
                                    name: 'Restore',
                                    text: __('Restore'),
                                    classname: 'btn btn-xs btn-info btn-ajax btn-restoreit',
                                    icon: 'fa fa-rotate-left',
                                    url: 'business/privatesea/restore',
                                    refresh: true
                                },
                                {
                                    name: 'Destroy',
                                    text: __('Destroy'),
                                    classname: 'btn btn-xs btn-danger btn-ajax btn-destroyit',
                                    icon: 'fa fa-times',
                                    url: 'business/privatesea/destroy',
                                    refresh: true
                                }
                            ],
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },

        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
